package cn.yeyiu.netonlinevideo.parser.video;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.yeyiu.netonlinevideo.bean.SourceData;
import cn.yeyiu.netonlinevideo.parser.Parser;
import cn.yeyiu.netonlinevideo.viewmodel.DataListViewModel;
import cn.yeyiu.util.NetUtil;
import cn.yeyiu.util.bean.MediaData;
import cn.yeyiu.util.bean.VideoDetail;
import cn.yeyiu.util.bean.VideoInfo;

public class AgefansParser extends Parser {
    public AgefansParser(SourceData source) {
        super(source);
    }

    @Override
    public Object getBaseData(String type, int pageIndex) {
        String vtype = getArgv(type, "vtype");
        String html = NetUtil.getByUrl(source.url + "/catalog/" + String.format(vtype, pageIndex + 1));
        return Jsoup.parse(html);
    }

    @Override
    public int getMaxCount(Object data) {
        Document document = (Document) data;
        String countStr = document.getElementById("result_count").text();
        Matcher m = Pattern.compile("(\\d+)").matcher(countStr);
        m.find();
        String n = m.group();
        return Integer.parseInt(n);
    }

    @Override
    public List<MediaData> getDataList(Object data, String type) {
        Document document = (Document) data;
        Elements uls = document.selectXpath("//div[@class='blockcontent1']");
        if (uls.size() == 0) {
            return null;
        }
        Element ul = uls.get(0);

        List<MediaData> dataList = new ArrayList<>();
        MediaData mediaData;
        VideoDetail videoDetail;
        for (Element li : ul.selectXpath("./div/div")) {
            videoDetail = new VideoDetail();
            mediaData = new MediaData();

            mediaData.code = source.code;
            mediaData.detail = source.url + li.selectXpath("./div/a").get(0).attr("href");
            mediaData.title = li.selectXpath("./div/div/div[1]/a").get(0).text();
            mediaData.logo = li.selectXpath("./div/a/img").get(0).attr("src");
            if (!mediaData.logo.startsWith("http")) {
                mediaData.logo = "https:" + mediaData.logo;
            }

            videoDetail.description = li.selectXpath("./div/div/div[2]/div[last()]/div").get(0).text();
            videoDetail.title = mediaData.title;
            videoDetail.logo = mediaData.logo;
            mediaData.data = videoDetail;
            dataList.add(mediaData);
        }
        return dataList;
    }

    @Override
    public Object buildData(MediaData mediaData) {
        VideoDetail videoDetail = (VideoDetail) mediaData.data;
        videoDetail.eps = new ArrayList<>();
        String html = NetUtil.getByUrl(mediaData.detail);
        Document document = Jsoup.parse(html);

        Elements uls = document.selectXpath("//div[@class='movurl' and contains(@style,'block')]/ul");
        if (uls.size() == 0) {
            return videoDetail;
        }
        Element ul = uls.get(0);
        VideoInfo videoInfo;
        for (Element li : ul.getElementsByTag("li")) {
            videoInfo = new VideoInfo();

            videoInfo.info = "web";
            videoInfo.title = li.getElementsByTag("a").first().text();
            videoInfo.videoSrc = source.url + li.getElementsByTag("a").first().attr("href");
            videoDetail.eps.add(videoInfo);
        }
        return videoDetail;
    }
}
